//
//  Loan_ApplyViewController.m
//  JinFuThree
//
//  Created by 爱尚 on 2018/9/5.
//  Copyright © 2018年 毛韶谦. All rights reserved.
//

#import "Loan_ApplyViewController.h"
#import "Loan_ApplyMainView.h"
#import "Loan_SelectBankcardView.h"
#import "Loan_repaymentView.h"
#import "Loan_BaseInfoViewController.h"

@interface Loan_ApplyViewController ()<Loan_ApplyMainViewDelegate,UIPickerViewDelegate,UIPickerViewDataSource,UITextFieldDelegate,Loan_SelectBankcardViewDelegate>
@property(nonatomic,strong)UIScrollView *myScrollView;
@property(nonatomic,strong)Loan_ApplyMainView *mainView;
@property(nonatomic,strong)UILabel *iconLabel;
@property(nonatomic,strong)UILabel *tipLabel;
@property(nonatomic,strong)UIButton *allBtn;
@property(nonatomic,strong)MSQTextField *numberTextField;

@property(nonatomic,strong)NSArray *InstallmentPeriodsArray;//期数数组
@property(nonatomic,strong)NSArray *repaymentWayArray;//分期方式数组
@property(nonatomic,strong)NSArray *loanPurposeArray;//借款用途
@property(nonatomic,strong)NSArray *bankCardArray;//银行卡列表

@property(nonatomic,strong)UIPickerView *pickerView;//选择器


@end

@implementation Loan_ApplyViewController

{
    NSString *userName;
    NSInteger select_index;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"申请借款";
    self.view.backgroundColor = ListMaoBackgroundColor;
    [self initViews];
    
}

#pragma mark ---------mainDelegate-------------
- (void)showBankPeakView:(Loan_RightImageView *)view{
    
    switch (view.tag) {
        case 1:
            if (self.numberTextField.text.length == 0) {
                [HUD_Mao showCharacterMessage:@"请先输入贷款金额"];
                return;
            }else{
                [self showPickerViewTag:1];
            }
            
            
            break;
        case 2:
            if (self.numberTextField.text.length == 0) {
                [HUD_Mao showCharacterMessage:@"请先输入贷款金额"];
                return;
            }else if (self.mainView.InstallmentPeriods.writeTextField.text.length == 0){
                [HUD_Mao showCharacterMessage:@"请先选择分期期数"];
                return;
            }else{
                [self showPickerViewTag:2];
            }
            
            
            break;
        case 3:
            if (self.numberTextField.text.length == 0) {
                [HUD_Mao showCharacterMessage:@"请先输入贷款金额"];
                return;
            }else if (self.mainView.InstallmentPeriods.writeTextField.text.length == 0){
                [HUD_Mao showCharacterMessage:@"请先选择分期期数"];
            }else if (self.mainView.repaymentWay.writeTextField.text.length == 0){
                [HUD_Mao showCharacterMessage:@"请先选择分期方式"];
                return;
            }else{
                [self showPickerViewTag:3];
            }
            
            
            break;
        case 4:
            if (self.numberTextField.text.length == 0) {
                [HUD_Mao showCharacterMessage:@"请先输入贷款金额"];
                return;
            }else if (self.mainView.InstallmentPeriods.writeTextField.text.length == 0){
                [HUD_Mao showCharacterMessage:@"请先选择分期期数"];
                return;
            }else if (self.mainView.repaymentWay.writeTextField.text.length == 0){
                [HUD_Mao showCharacterMessage:@"请先选择分期方式"];
                return;
            }else if (self.mainView.loanPurpose.writeTextField.text.length == 0){
                [HUD_Mao showCharacterMessage:@"请先选择分期方式"];
                return;
            }else{
                self.mainView.shouldPay.writeTextField.text = @"¥416.66";
                Loan_SelectBankcardView *selectBankcardView = [[Loan_SelectBankcardView alloc]init];
                selectBankcardView.selectBankcardViewDelegate = self;
                [selectBankcardView show:self.bankCardArray];

                
            }
            
            break;
            
            case 5:
            if (self.numberTextField.text.length == 0) {
                [HUD_Mao showCharacterMessage:@"请先输入贷款金额"];
                return;
            }{
                Loan_repaymentView *repaymentView = [[Loan_repaymentView alloc]init];
                [repaymentView show:@[@"22",@"22",@"22",@"22"]];
            }
            
            
            break;
            
        default:
            
            
            break;
    }
}

-(void)sureBtnAction:(UIButton *)sender{
    self.mainView.collectionAccount.writeTextField.text = self.bankCardArray[sender.tag];
}

- (void)showPickerViewTag:(NSInteger)tag{
    [self.view endEditing:YES];
    UIView *backgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidthMao, ScreenHeight)];
    backgroundView.backgroundColor = [UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:0.4/1.0];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(cancleAction:)];
    [backgroundView addGestureRecognizer:tap];
    
    UIView *whiteView = [[UIView alloc] initWithFrame:CGRectMake(0, ScreenHeight - 240, ScreenHeight, 240)];
    whiteView.backgroundColor = [UIColor whiteColor];
    [backgroundView addSubview:whiteView];
    
    UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    leftBtn.frame = CGRectMake(80, 0, ScreenWidthMao - 80 - 50, 50);
    [whiteView addSubview:leftBtn];
    
    UILabel *leftLabel = [[UILabel alloc] init];
    leftLabel.frame = CGRectMake(0, 0, 80, 50);
    [leftLabel setTextAlignment:NSTextAlignmentCenter];
    leftLabel.text = @"取消";
    leftLabel.textColor = HexColor(0x454444);
    leftLabel.font = [UIFont boldSystemFontOfSize:16];
    [whiteView addSubview:leftLabel];
    
    UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    rightBtn.frame = CGRectMake(ScreenWidthMao - 80, 0, 80, 50);
    rightBtn.tag = tag;
    [rightBtn setTitle:@"确定" forState:UIControlStateNormal];
    [rightBtn setTitleColor:ThemeMaoColor forState:UIControlStateNormal];
    rightBtn.titleLabel.font = [UIFont boldSystemFontOfSize:16];
    [rightBtn addTarget:self action:@selector(select_sureBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    
    [whiteView addSubview:rightBtn];
    
    
    _pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 30, ScreenWidthMao, 190)];
    _pickerView.tag = tag;
    _pickerView.delegate = self;
    _pickerView.dataSource = self;
    [whiteView addSubview:_pickerView];
    
    [UIView animateWithDuration:0.3 animations:^{
        [[HFQCommonTools lastWindow] addSubview:backgroundView];
    }];
    
    
    

}

-(void)sureBtnAction{
    
    Loan_BaseInfoViewController *baseVC = [[Loan_BaseInfoViewController alloc]init];
    baseVC.view.backgroundColor = ListMaoBackgroundColor;
    [self.navigationController pushViewController:baseVC animated:YES];
}

- (void)select_sureBtnAction:(UIButton *)sender{
    switch (sender.tag) {
        case 1:
            
            self.mainView.InstallmentPeriods.writeTextField.text = self.InstallmentPeriodsArray[select_index];
            
            
            break;
            
        case 2:
            
            self.mainView.repaymentWay.writeTextField.text = self.repaymentWayArray[select_index];
            
            
            break;
        case 3:
            
            self.mainView.loanPurpose.writeTextField.text = self.loanPurposeArray[select_index];
            
            
            break;
            
        default:
            break;
    }
    UIView *whiteView = sender.superview;
    UIView *backgroundView = whiteView.superview;
    
    [UIView animateWithDuration:0.3 animations:^{
        backgroundView.alpha = 0;
    } completion:^(BOOL finished) {
        backgroundView.hidden = YES;
        [backgroundView removeFromSuperview];
    }];
}

- (void)cancleAction:(UITapGestureRecognizer *)gesture {
    
    UIView *view = gesture.view;
    [UIView animateWithDuration:0.3 animations:^{
        
        view.alpha = 0;
    } completion:^(BOOL finished) {
        view.hidden = YES;
        [view removeFromSuperview];
    }];
}

#pragma mark --------------Picker 代理方法---------------

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    
    if (pickerView.tag == 1) {
        return self.InstallmentPeriodsArray.count;
    }
    
    if (pickerView.tag == 2) {
        return self.repaymentWayArray.count;
    }else{
        return self.loanPurposeArray.count;
    }
    
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    
    if (pickerView.tag == 1) {
        return self.InstallmentPeriodsArray[row];
    }
    
    if (pickerView.tag == 2) {
        return self.repaymentWayArray[row];
    }else{
        return self.loanPurposeArray[row];
    }
    return @"";
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    select_index = row;
    
}

#pragma mark ---------textfieldDelegate------------------

- (void)textFieldEditingChange:(UITextField *)textField {
    
    if ([HFQCommonTools validateProductPrice:textField.text]) {
        
    }else {
        
        textField.text = [textField.text substringToIndex:textField.text.length-1];
    }
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    NSLog(@"%@===%lu===%@",textField.text,(unsigned long)range.location,string);
    
    if ([self validateProductPrice:string] || !string.length) {
        return YES;
    }
    return NO;
}

- (BOOL) validateProductPrice:(NSString *)price {
    
    NSString *stringRegex = @"^(([0-9])|([.]))$";
    NSPredicate *passWordPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",stringRegex];
    return [passWordPredicate evaluateWithObject:price];
}

#pragma mark ---------布局页面-------------------------------------
-(void)initViews{
    UIView *contentView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidthMao, 700)];
    contentView.backgroundColor = [UIColor whiteColor];
    
    UIView *progressView = [[UIView alloc]init];
    progressView.backgroundColor = ThemeMaoColor;
    [self.view addSubview:progressView];
    [progressView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.mas_equalTo(self.view);
        make.width.mas_offset(@(ScreenWidthMao/6*1));
        make.height.mas_offset(@2);
    }];
    
    _iconLabel = [[UILabel alloc]init];
    _iconLabel.font = [UIFont systemFontOfSize:15];
    _iconLabel.text = @"￥";
    [contentView addSubview:_iconLabel];
    [_iconLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(contentView.mas_left).offset(25);
        make.width.mas_offset(@15);
        make.top.mas_equalTo(contentView.mas_top).offset(50);
        make.height.mas_offset(@15);
    }];
    
    _numberTextField = [[MSQTextField alloc]init];
    _numberTextField.font = [UIFont boldSystemFontOfSize:24];
    _numberTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
    _numberTextField.placeholder = @"请输入金额(100的整数倍)";
    _numberTextField.delegate = self;
    [contentView addSubview:_numberTextField];
    [_numberTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.iconLabel.mas_right);
        make.bottom.mas_equalTo(self.iconLabel.mas_bottom).offset(14);
        make.right.mas_equalTo(contentView.mas_right).offset(-25);
        make.top.mas_equalTo(contentView.mas_top).offset(30);
    }];
    
    UIView *line = [[UIView alloc]init];
    line.backgroundColor = ThemeMaoLineColor;
    [contentView addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.iconLabel);
        make.right.mas_equalTo(self.numberTextField);
        make.height.mas_offset(@0.5);
        make.top.mas_equalTo(self.iconLabel.mas_bottom).offset(15);
    }];
    
    _tipLabel = [[UILabel alloc]init];
    _tipLabel.text = @"1000起借，最多可借20000元";
    _tipLabel.font = [UIFont systemFontOfSize:12];
    _tipLabel.textColor = HexColor(0x666666);
    [contentView addSubview:_tipLabel];
    [_tipLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.iconLabel);
        make.right.mas_equalTo(contentView.mas_right).offset(-100);
        make.top.mas_equalTo(line.mas_bottom).offset(10);
        make.height.mas_offset(@13);
    }];
    
    _allBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [_allBtn setTitle:@"全部借出" forState:UIControlStateNormal];
    [_allBtn setTitleColor:HexColor(0xC3A978) forState:UIControlStateNormal];
    _allBtn.titleLabel.font = [UIFont systemFontOfSize:12];
    [_allBtn addTarget:self action:@selector(allBtnAction) forControlEvents:UIControlEventTouchUpInside];
    [contentView addSubview:_allBtn];
    [_allBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.height.mas_equalTo(self.tipLabel);
        make.right.mas_equalTo(line);
    }];
    
    UIView *spaceView = [[UIView alloc]init];
    spaceView.backgroundColor = ListMaoBackgroundColor;
    [contentView addSubview:spaceView];
    [spaceView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(contentView);
        make.top.mas_equalTo(self.tipLabel.mas_bottom).offset(32);
        make.height.mas_offset(@8);
    }];
    [contentView addSubview:self.mainView];
    [_mainView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(contentView);
        make.top.mas_equalTo(spaceView.mas_bottom);
        make.height.mas_offset(@420);
        
    }];
    self.myScrollView = [[UIScrollView alloc]initWithFrame:CGRectZero];
    self.myScrollView.contentSize = contentView.frame.size;
    self.myScrollView.showsVerticalScrollIndicator = NO;
    self.myScrollView.backgroundColor = RGBCOLOR(242, 242, 242, 1);
    [self.myScrollView addSubview:contentView];
    [self.view addSubview:self.myScrollView];
    [self.myScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(self.view);
        make.top.mas_equalTo(self.view).offset(2);
        make.bottom.mas_equalTo(self.view).offset(0);
    }];
}
-(void)allBtnAction{
    
}

-(NSArray *)InstallmentPeriodsArray{
    if (!_InstallmentPeriodsArray) {
        _InstallmentPeriodsArray = [[NSArray alloc]initWithObjects:@"3期",@"6期",@"9期",@"12期", nil];
    }
    return _InstallmentPeriodsArray;
}

-(NSArray *)repaymentWayArray{
    if (!_repaymentWayArray) {
        _repaymentWayArray = [[NSArray alloc]initWithObjects:@"等额本息",@"等本等息", nil];
    }
    return _repaymentWayArray;
}

-(NSArray *)loanPurposeArray{
    if (!_loanPurposeArray) {
        _loanPurposeArray = [[NSArray alloc]initWithObjects:@"买手机",@"租房", nil];
    }
    return _loanPurposeArray;
}

-(NSArray *)bankCardArray{
    if (!_bankCardArray) {
        _bankCardArray = [[NSArray alloc]initWithObjects:@"招商银行(2601)",@"中国工商银行(2345)", nil];
    }
    return _bankCardArray;
}

-(Loan_ApplyMainView *)mainView{
    if (!_mainView) {
        _mainView = [[Loan_ApplyMainView alloc]init];
        _mainView.mainViewDelegate = self;
       
    }
    return _mainView;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
